#include "lv_conf.h"

#if USE_LV_BLUETOOTH != 0

#ifndef ASR_RTL8763BO_PORT
#define ASR_RTL8763BO_PORT

///#include "cp_include.h"
///#include "plat_types.h"
///#include "ui_log_api.h"
#include "bt_uart.h"
#include "bt_uart_api.h"
///#include "tm.h"
///#include "ui_os_api.h"
#include "lvgl/hal/hal.h"
#include "list.h"

#define LOG_TAG "RTL8763"

#define MODLOG_GLOBAL_LEVEL 500

typedef enum _ModLogLevel {
    MODLOG_LEVEL_ERROR        = 100,
    MODLOG_LEVEL_WARNING      = 200,
    MODLOG_LEVEL_INFO         = 300,
    MODLOG_LEVEL_DEBUG        = 400,
    MODLOG_LEVEL_VERBOSE      = 500
} ModLogLevel;

#ifndef MOD_LOG_PRINT
#define MOD_LOG_PRINT(logLevel, levelName, format, ...)\
    do {\
        if (logLevel <= MODLOG_GLOBAL_LEVEL) {\
            printf("["LOG_TAG"]" levelName format "\n" , ##__VA_ARGS__);\
        }\
    } while(0)
#endif

#if 0
#define MODLOGE(fmt, ...) MOD_LOG_PRINT(MODLOG_LEVEL_ERROR, "ERR: ", fmt, ##__VA_ARGS__)
#define MODLOGW(fmt, ...) MOD_LOG_PRINT(MODLOG_LEVEL_WARNING, "WNG: ", fmt, ##__VA_ARGS__)
#define MODLOGI(fmt, ...) MOD_LOG_PRINT(MODLOG_LEVEL_INFO, "INF: ", fmt, ##__VA_ARGS__)
#define MODLOGD(fmt, ...) MOD_LOG_PRINT(MODLOG_LEVEL_DEBUG, "DBG: ", fmt, ##__VA_ARGS__)
#define MODLOGV(fmt, ...) MOD_LOG_PRINT(MODLOG_LEVEL_VERBOSE, "VBS: ", fmt, ##__VA_ARGS__)
#else
#if 1
#define MODLOGE(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
#define MODLOGW(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
#define MODLOGI(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
#define MODLOGD(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
#define MODLOGV(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
#else
#define MODLOGE(fmt, ...)
#define MODLOGW(fmt, ...)
#define MODLOGI(fmt, ...)
#define MODLOGD(fmt, ...)
#define MODLOGV(fmt, ...)
#endif
#endif

#define APP_PRINT_ERROR0 MODLOGE
#define APP_PRINT_ERROR1 MODLOGE
#define APP_PRINT_WARN0(fmt, ...)
#define APP_PRINT_WARN1(fmt, ...)
#define APP_PRINT_INFO0(fmt, ...)
#define APP_PRINT_INFO1(fmt, ...)
#define APP_PRINT_INFO2(fmt, ...)
#define APP_PRINT_INFO3(fmt, ...)
#define DBG_DIRECT ///raw_uart_log


typedef struct
{
    uint8_t paired_num;
    char newest_addr[6];
    bool paired_flag;
} T_PAIRED_INFO;

typedef struct
{
    char bbpro_device_status;
    char bbpro_hci_link_status;
    T_PAIRED_INFO paired_info;
} T_BT_STATUS;


// EVENT_MODE_STATE parameters
// mode
enum {
    BOM_PLAY_MODE_A2DP_SINK      = 0x00,
    BOM_PLAY_MODE_A2DP_SOURCE    = 0x01,
    BOM_PLAY_MODE_LOCAL_PLAYBACK = 0x02,
};
// state
enum {
    APP_PLAY_MODE_INVALID         = 0x00,
    APP_PLAY_MODE_INITIALIZING    = 0x01,
    APP_PLAY_MODE_READY           = 0x02,
};

// EVENT_LINK_STATUS: report link(ACL) status to MCU
// link_status
enum {
    LINK_STATUS_CONNECTED       = 0x00,
    LINK_STATUS_DISCONNECTED    = 0x01,
    LINK_STATUS_FAIL            = 0x02,
};
// param, valid only when LINK_STATUS_DISCONNECTED
enum {
    LINK_DISCONNECTED_REASON_PAIR_CANCEL    = 0x0113,
    LINK_DISCONNECTED_REASON_BT_CLOSE       = 0x0116,
};

// EVENT_CALL_STATUS
enum {
    CALL_STATUS_CALL_IDLE                        = 0x00,
    CALL_STATUS_VOICE_ACTIVATION_ONGOING         = 0x01,
    CALL_STATUS_INCOMING_CALL_ONGOING            = 0x02,
    CALL_STATUS_OUTCOMING_CALL_ONGOING           = 0x03,
    CALL_STATUS_CALL_ACTIVE                      = 0x04,
    CALL_STATUS_CALL_ACTIVE_WITH_CALL_WAITING    = 0x05,
    CALL_STATUS_CALL_ACTIVE_WITH_CALL_HODE       = 0x06,
};

#define false   0
#define true    1

#define BBPRO_MUSIC_PLAY 0x0
#define INQUIRY_COMPLETE 0x0

#define TRACE_BINARY(a, b) 0
#define TRACE_BDADDR(a) 0

#define DATATRANS_UART_DMA

#define DATATRANS_UART_BAUDRATE BAUDRATE_3000000

///#define DEBUG_UART_RX_RAW_PACKET
///#define DEBUG_UART_TX_RAW_PACKET

///#define DEBUG_UART_RX_HCI_PACKET
///#define DEBUG_UART_TX_HCI_PACKET

///#define DEBUG_SBC_FRAME_TIMEOUT_RAW_DATA

///#define SEND_EVENT_TO_RECV_TASK_WITH_PARAM


/* AUDIO_PATH_SELECT */
#define AUDIO_PATH_TX_RX_NORMAL 0x1000
#define AUDIO_PATH_TX_RX_RBUF   0x1001  // software loopback
#define AUDIO_PATH_TX_RX_UART   0x1002  // hardware loopback


/* AUDIO_TX_SELECT */
#define AUDIO_TX_AUDIOHAL       0x2000
#define AUDIO_TX_LOCAL_BUFFER   0x2001
#define AUDIO_TX_LOCAL_FILE     0x2002


/* AUDIO_RX_SELECT */
#define AUDIO_RX_AUDIOHAL       0x3000
#define AUDIO_RX_LOCAL_BUFFER   0x3001


/* select one */
#define AUDIO_PLAY_NORMAL                   // pass
//#define AUDIO_PLAY_LOCAL_BUFFER_TX_RX_RBUF  // pass
//#define AUDIO_PLAY_LOCAL_BUFFER_TX_RX_UART  // pass
//#define AUDIO_PLAY_LOCAL_FILE_TX_RX_RBUF    // pass
//#define AUDIO_PLAY_LOCAL_FILE_TX_RX_UART    // pass
//#define AUDIO_PLAY_LOCAL_SPEECH_TX_RX_RBUF  // pass
//#define AUDIO_PLAY_LOCAL_SPEECH_TX_RX_UART  // pass


/* some claasic play path */
#if defined AUDIO_PLAY_NORMAL
#define AUDIO_PATH_SELECT    AUDIO_PATH_TX_RX_NORMAL
#define AUDIO_TX_SELECT      AUDIO_TX_AUDIOHAL
#define AUDIO_RX_SELECT      AUDIO_RX_AUDIOHAL
#elif defined AUDIO_PLAY_LOCAL_BUFFER_TX_RX_RBUF
#define AUDIO_PATH_SELECT    AUDIO_PATH_TX_RX_RBUF
#define AUDIO_TX_SELECT      AUDIO_TX_LOCAL_BUFFER
#define AUDIO_RX_SELECT      AUDIO_RX_LOCAL_BUFFER
#elif defined AUDIO_PLAY_LOCAL_BUFFER_TX_RX_UART
#define AUDIO_PATH_SELECT    AUDIO_PATH_TX_RX_UART
#define AUDIO_TX_SELECT      AUDIO_TX_LOCAL_BUFFER
#define AUDIO_RX_SELECT      AUDIO_RX_LOCAL_BUFFER
#elif defined AUDIO_PLAY_LOCAL_FILE_TX_RX_RBUF
#define AUDIO_PATH_SELECT    AUDIO_PATH_TX_RX_RBUF
#define AUDIO_TX_SELECT      AUDIO_TX_LOCAL_FILE
#define AUDIO_RX_SELECT      AUDIO_RX_AUDIOHAL
#elif defined AUDIO_PLAY_LOCAL_FILE_TX_RX_UART
#define AUDIO_PATH_SELECT    AUDIO_PATH_TX_RX_UART
#define AUDIO_TX_SELECT      AUDIO_TX_LOCAL_FILE
#define AUDIO_RX_SELECT      AUDIO_RX_AUDIOHAL
#elif defined AUDIO_PLAY_LOCAL_SPEECH_TX_RX_RBUF
#define AUDIO_PATH_SELECT    AUDIO_PATH_TX_RX_RBUF
#define AUDIO_TX_SELECT      AUDIO_TX_AUDIOHAL
#define AUDIO_RX_SELECT      AUDIO_RX_AUDIOHAL
#elif defined AUDIO_PLAY_LOCAL_SPEECH_TX_RX_UART
#define AUDIO_PATH_SELECT    AUDIO_PATH_TX_RX_UART
#define AUDIO_TX_SELECT      AUDIO_TX_AUDIOHAL
#define AUDIO_RX_SELECT      AUDIO_RX_AUDIOHAL
#endif

typedef struct
{
    volatile uint16_t   id;
    volatile uint16_t   readIndex;          /* index of read queue */
    volatile uint16_t   writeIndex;         /* index of write queue */
    volatile uint16_t   len;
} RxEvent_TypeDef;


void uart_rx_lock(void);
void uart_rx_unlock(void);

struct sbc_frame_mgr {
    struct list_head frame_head;
    unsigned int num;
    uint8_t queue_lock;
};

struct sbc_frame {
    struct list_head node;
    void *data;
    uint8_t n_frames;
    uint16_t n_bytes;
};


#endif /* ASR_RTL8763BO_PORT */
#endif /* USE_LV_BLUETOOTH */
